package com.lenovo.smarttraffic.ui.activity.stage5;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.SensorData;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;

import java.util.HashMap;
import java.util.Map;

public class RoadFind2Activity extends BaseActivity {

    public static Map<String, Object> data = new HashMap<>();

    private Handler handler = new Handler();

    private TextView info_tv, road_1, road_2, road_3;
    private View color1, color2, color3;

    @Override
    protected int getLayout() {
        return R.layout.activity_road_find2;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(this, "路况查询");

        info_tv = findViewById(R.id.info_tv);
        road_1 = findViewById(R.id.road_1);
        road_2 = findViewById(R.id.road_2);
        road_3 = findViewById(R.id.road_3);
        color1 = findViewById(R.id.color1);
        color2 = findViewById(R.id.color2);
        color3 = findViewById(R.id.color3);

        runnable.run();
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            HttpUtils.post("get_all_sense", new HttpUtils().add("UserName", MainActivity.userName), response -> {
                SensorData sensorData = new Gson().fromJson(response.toString(), SensorData.class);
                info_tv.setText("室外空气指数\nPM2.5：" + sensorData.getPM25() + "ug/m3\n温度：" + sensorData.getTemperature() + "℃\n湿度：" + sensorData.getHumidity() + "%");
            });

            for (int i = 1; i <= 3; i++) {
                int finalI = i;
                HttpUtils.post("get_road_status", new HttpUtils().add("RoadId", i).add("UserName", MainActivity.userName),
                        response -> {
                            Map map = new Gson().fromJson(response.toString(), Map.class);
                            int status = ((Double) map.get("Status")).intValue();
                            String colorStr = "#fff", state = "";

                            if (status == 1) {
                                colorStr = "#0ebd12";
                                state = "通畅";
                            } else if (status == 2) {
                                colorStr = "#98ed1f";
                                state = "较通畅";
                            } else if (status == 3) {
                                colorStr = "#ffff01";
                                state = "拥挤";
                            } else if (status == 4) {
                                colorStr = "#ff0103";
                                state = "堵塞";
                            } else if (status == 5) {
                                colorStr = "#4c060e";
                                state = "爆表";
                            }
                            int color = Color.parseColor(colorStr);

                            if (finalI == 1) {
                                color1.setBackgroundColor(color);
                                road_1.setText("1号道路：" + state);
                            } else if (finalI == 2) {
                                color2.setBackgroundColor(color);
                                road_2.setText("2号道路：" + state);
                            } else {
                                color3.setBackgroundColor(color);
                                road_3.setText("3号道路：" + state);
                            }
                        });
            }

            handler.postDelayed(runnable,3000);
        }
    };
}
